# 业务层view
import orm

def login():
    path = r'users.json'
    count = 0
    while True:
        tag = 0
        name = input('请输入用户名：')
        pwd = input('请输入密码：')
        user_list = orm.read_data(path)
        for user in user_list:
            if name == user['username'] and pwd == user['password']:
                print('登陆成功')
                tag = 1
        if tag == 0:
            count += 1
            if count == 3:
                print("用户已被锁定")
                break
            else:
                print(f"用户名或密码错误，请重新输入！还剩{3 - count}次登录机会")
                continue
        else:
            break
    return tag

def register():
    path = r'users.json'
    while True:
        name = input("请输入想要注册的用户名：")
        pwd1 = input("请输入密码：")
        pwd2 = input("请再次输入密码：")
        if pwd1 == pwd2:
            new_user = {'username': name, 'password': pwd2}
            user_list = orm.read_data(path)
            user_list.append(new_user)
            orm.write_data(user_list, path)
            print('注册成功')
            tag = 1
            # break
        else:
            print("两次密码不一致，请重新输入")
            continue
        return tag

if __name__ == '__main__':
    # print(register())
    print(login())